package com.leosoft.heatmonitor.service.transactional;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.leosoft.heatmonitor.dao.ProjectDAO;
import com.leosoft.heatmonitor.dao.RoleDAO;
import com.leosoft.heatmonitor.dao.UserDAO;
import com.leosoft.heatmonitor.model.Project;
import com.leosoft.heatmonitor.model.Role;
import com.leosoft.heatmonitor.model.User;

@Component("userService")
public class UserService {

	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private ProjectDAO projectDAO;

	public void update(User user) {
		userDAO.saveUser(user);
	}

	public String getRoleByName(String username) {
		return userDAO.getRoleByName(username);
	}

	public Map<String, Object> list(int startIndex, int length, String searchValue, List<String> wherePart,
			List<String[]> odrerPart) {
		return userDAO.list(startIndex, length, searchValue, wherePart, odrerPart);
	}

	public Map<String, Object> load(String id) {
		User user = userDAO.load(id);
		Map<String, Object> result = new HashMap<>();
		result.put("id", user.getId());
		result.put("username", user.getUsername());
		result.put("nickname", user.getNickname());
		if (user.getRoles().size() > 0) {
			result.put("roleId", user.getRoles().get(0).getId());
			result.put("roleName", user.getRoles().get(0).getRole());
		}
		return result;
	}

	public void create(String username, String nickname, String roleId) {
		User user = new User();
		List<Role> roles = new ArrayList<Role>();
		Role role = roleDAO.getById(roleId);
		roles.add(role);
		user.setUsername(username);
		user.setPassword("202cb962ac59075b964b07152d234b70");
		user.setNickname(nickname);
		user.setCreatetime(new Timestamp(System.currentTimeMillis()));
		user.setRoles(roles);
		user.setEnabled(true);
		userDAO.saveUser(user);
	}

	public void update(String id, String username, String nickname, String roleId) {
		User user = userDAO.load(id);
		List<Role> roles = new ArrayList<Role>();
		Role role = roleDAO.getById(roleId);
		roles.add(role);
		user.setUsername(username);
		user.setNickname(nickname);
		user.setRoles(roles);
		user.setEnabled(true);
		userDAO.saveUser(user);
	}

	public void delete(String id) {
		userDAO.delete(id);

	}

	public boolean exsit(String username) {
		User user = userDAO.getUserByName(username);
		if (null == user) {
			return false;
		}
		return true;
	}

	public User getUserByName(String username) {

		return userDAO.getUserByName(username);
	}

	public List<Map<String, String>> getProject(String id) {
		return userDAO.getProject(id);
	}

	public void control(String userId, String[] projectIds) {
		List<Project> projects = new ArrayList();
		for (String projectId : projectIds) {
			Project project = projectDAO.getProjectById(projectId);
			projects.add(project);
		}
		userDAO.control(userId, projects);
	}

	public boolean check(String username, String old) {
		User user = userDAO.getUserByName(username);
		String oldP = encryption(old);
		if (user.getPassword().equals(oldP)) {
			return true;
		}
		return false;
	}

	public void changePwd(String username, String newp) {
		User user = userDAO.getUserByName(username);
		user.setPassword(encryption(newp));
		userDAO.saveUser(user);
	}

	public String encryption(String plainText) {
		String re_md5 = new String();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			re_md5 = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return re_md5;
	}

	public void reset(String id) {
		User user = userDAO.load(id);
		user.setPassword("202cb962ac59075b964b07152d234b70");
		userDAO.update(user);
	}
}
